﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DO.DreamOnTrader.Quote;

namespace DO.DreamOnTrader.Indicator
{
	public enum KBarOption { OPEN = 0, HIGH, LOW, CLOSE, VOL, TIME }

	/// <summary>
	/// K棒指標
	/// </summary>
	public class KBarIndecator : TechnicalIndicator, IActiveIndicator
	{
		public int HuringMin { get; private set; }
		/// <summary>
		/// 取得此指標要取得k棒的什麼值
		/// </summary>
		public KBarOption getItem { get; private set; }

		float Value;

		DateTime ktime;

		public static KBarIndecator New(int _huringMin, KBarOption _item)
		{
			KBarIndecator tech = new KBarIndecator(_huringMin,_item);
			TechnicalIndicator.Add(_huringMin, tech);
			return tech;
		}

		private KBarIndecator(int _huringMin, KBarOption _item)
		{
			HuringMin = _huringMin;
			getItem = _item;
			Value = float.NaN;
		}

		private KBarIndecator(int _huringMin) : this(_huringMin, KBarOption.CLOSE) { }

		protected override string[] Keys
		{
			get { return new string[] { "HuringMin","getItem" }; }
		}

		protected override void Formula(KBar kbar)
		{
			ktime = kbar.Ktime;

			switch (getItem)
			{
				case KBarOption.OPEN:
					Value = kbar.OpenPrice;
					break;
				case KBarOption.HIGH:
					Value = kbar.HPrice;
					break;
				case KBarOption.LOW:
					Value = kbar.LPrice;
					break;
				case KBarOption.CLOSE:
					Value = kbar.ClosePrice;
					break;
				case KBarOption.VOL:
					Value = (float)kbar.Vol;
					break;
			}

			if (GetValueEvent != null)
				GetValueEvent(getValue(), this);
		}

		public override float getValue()
		{
			return Value;
		}

		public override string ToString()
		{
			return Label() + " " + ktime.ToString() + ":" + getValue();
		}

		#region IActiveIndicator 成員

		public event getValueEventHandler GetValueEvent;

		#endregion
	}
}
